<template>
    <div class="tabpanel_tab_content nav-tab" style="width:auto;right:0">
        <div class="tabpanel_left_scroll" @click="movLeftOneTab" v-show="leftShow"></div>
        <div ref="tab_content_scroll" class="tabpanel_move_content tabpanel_move_content_scroll" style="width:auto">
            <ul ref="tab_content" class="tabpanel_mover" :style="'margin-left: ' + marginLeft + 'px'">
                <li style="-moz-user-select:none;" onselectstart="return false;" :class="currentPath == '/dashboard/workplace' ? 'active' : ''">
                    <div class="title" @click="goPage('/dashboard/workplace')">
                        <i class="fa fa-home"></i> 仪表盘
                    </div>
                    <div class="refresher"></div>
                    <div class="closer display_none"></div>
                </li>
                <li v-for="(item,index) in tabList" :key="(item.meta && item.meta.key) || index" style="-moz-user-select:none;" onselectstart="return false;" :class="currentPath == item.path ? 'active' : ''">
                    <div class="title" :title="item.meta.name" @click="goPage(item.path,item)">{{item.meta.name}}</div>
                    <div class="refresher"></div>
                    <div class="closer" @click="closeTab(item.meta.key)"></div>
                </li>
            </ul>
        </div>
        <div class="tabpanel_right_scroll" @click="movRightOneTab" v-show="rightShow"></div>
        <div class="tabpanel_tab_spacer"></div>
    </div>
</template>

<script>
import { mapState } from "vuex";

export default {
    name: "NavTab",
    data () {
        return {
            tabContentScroll: null,
            marginLeft: 0,
            leftShow: false,
            rightShow: false,
            tabpanelMoverML: "margin-left: 0px;",

        }
    },
    created () {

    },
    mounted () {
        this.tabContentScroll = this.$refs["tab_content_scroll"];
        // 监听窗口大小
        window.onresize = () => {
            return (this.checkShow)()
        }

    },
    computed: {

        ...mapState({
            // ...
            tabList: state => state.tab.tabList,
        }),
        currentPath () {
            return this.$route.path;
        }
    },
    methods: {
        goPage (url, item) {
            console.log(item, url);
            if (url && (url.includes('/crm/customer/detail/') || url.includes('/project/projectDetails') || url.includes('/finance/accounting/credentials'))) {
                this.$router.push({ path: item.fullPath });
            } else {
                this.$router.push({ path: url });
            }
        },
        closeTab (key) {
            this.$store.commit("DELETE_TAB", key);
            const newTabList = this.tabList.filter(f => f.key != key);
            //关闭之后，默认跳转到左后一个
            let url = '/dashboard/workplace';
            if (newTabList.length > 0) {
                url = newTabList[newTabList.length - 1].path;
            }
            console.log("closeTab_url", url)
            this.$router.push({ path: url });
        },
        movLeftOneTab () {
            let { marginLeft } = this;
            marginLeft += 106;
            this.checkShow(marginLeft);
        },
        movRightOneTab () {
            let { marginLeft } = this;
            marginLeft -= 106;
            this.checkShow(marginLeft);
        },
        //检查 
        checkShow (val) {
            const { tabContentScroll, tabList } = this;
            let { leftShow, rightShow, marginLeft } = this;
            let left = val || marginLeft;
            let tabLength = tabList.length * 106;
            let tabClientWidth = tabContentScroll.clientWidth;
            // console.log('菜单栏的宽度', tabContentScroll.clientWidth)
            //定位 选中的TAB
            if (!val) {
                let findIndex = tabList.findIndex(f => f.path == this.$route.path);
                // console.log('findIndex',findIndex)
                if (findIndex) {
                    left = tabClientWidth - (findIndex + 2) * 106;
                } else if (0 == findIndex) {
                    left = 0;
                }
            }
            if (left > 0) {
                left = 0;
            }
            let offWidth = tabLength - tabClientWidth + left + 106;
            leftShow = left != 0;
            rightShow = offWidth > 0;
            Object.assign(this, {
                marginLeft: left,
                leftShow,
                rightShow,
            })
        }
    },
    watch: {
        tabList: function (newValue, oldValue) {
            this.checkShow();
        },
    }

};
</script>

<style lang="less" scoped>
    //必须 保证 该顺序
    //1.
    .layout-navbar-fixed .tabpanel_tab_content {
        width: 100%;
        //   width:calc(100vw - 250px) !important;
    }
    //2.
    .sidebar-collapse .tabpanel_tab_content {
        width: calc(100vw) !important;
    }
    //3.
    .sidebar-open .tabpanel_tab_content {
        width: calc(100vw) !important;
    }
    //4.
    @media (min-width: 992px) {
        .sidebar-collapse .tabpanel_tab_content {
            width: calc(100vw - 4.6rem) !important;
        }
    }
    //5.
    .tabpanel_tab_content {
        height: 39px;
        line-height: 39px;
        background-color: white;
        border-bottom: 1px solid #e5e5e5;
        overflow: hidden;
        white-space: nowrap;
        position: relative;
    }
    .tabpanel_tab_content .tabpanel_move_content {
        width: 0;
        overflow: hidden;
    }
    .tabpanel_move_content_scroll {
        margin-left: 18px;
        margin-right: 18px;
    }
    .tabpanel_mover {
        width: 5000px;
        margin: 0;
        padding: 0;
        position: relative;
        transition: all 0.5s;
    }
    .tabpanel_mover li {
        width: 100px;
        line-height: 22px;
        padding: 1px 16px 2px 3px;
        margin: 6px 0 6px 6px;
        border: 1px solid #e4e4e4;
        background: #fff;
        box-shadow: 0 0 5px #e6e6e6;
        float: left;
        position: relative;
        list-style-type: none;
        cursor: pointer;
        overflow: hidden;
    }
    .tabpanel_mover li .refresher {
        display: none;
    }
    .tabpanel_mover li .closer {
        background: transparent url(../../assets/tab-close.png) no-repeat;
        position: absolute;
        right: 5px;
        top: 8px;
        width: 11px;
        height: 11px;
        cursor: pointer;
    }

    .tabpanel_mover li .closer:hover {
        background: transparent url(../../assets/tab-close.gif) no-repeat;
    }
    .tabpanel_mover li .title {
        font-size: 12px;
        padding-left: 5px;
        overflow: hidden;
        *height: 20px;
        *line-height: 20px;
        *margin-top: 2px;
        white-space: nowrap;
        text-overflow: ellipsis;
    }
    .tabpanel_mover li .title i {
        font-size: 18px;
        vertical-align: middle;
        margin-top: -2px;
    }
    .tabpanel_mover li div,
    .tabpanel_mover li a {
        color: #23508e;
    }
    .tabpanel_mover li .icon_title {
        font-size: 12px;
        color: #23508e;
        padding-left: 25px;
        background-repeat: no-repeat;
        background-position: 5px 4px;
        overflow: hidden;
        *height: 20px;
        *line-height: 20px;
        *margin-top: 2px;
        *background-position: 5px 1px;
    }
    .tabpanel_mover li.active {
        background-color: #00a0e9; //3aa0ff
    }
    .tabpanel_mover li.active div,
    .tabpanel_mover li.active a {
        color: #fff;
    }
    .tabpanel_tab_content .tabpanel_left_scroll {
        background: transparent url(../../assets/scroll-left.gif) no-repeat 0 0;
        position: absolute;
        width: 18px;
        height: 24px;
        left: 0;
        top: 8px;
        cursor: pointer;
        z-index: 10;
    }
    .tabpanel_tab_content .tabpanel_right_scroll {
        background: transparent url(../../assets/scroll-right.gif) no-repeat 0 0;
        position: absolute;
        width: 18px;
        height: 24px;
        right: 0;
        top: 8px;
        cursor: pointer;
        z-index: 10;
    }
    .tabpanel_tab_content .tabpanel_scroll_over {
        background-position: -18px 1px;
    }
    .tabpanel_tab_content .tabpanel_left_scroll_disabled {
        background-position: 0 0;
        opacity: 0.5;
        -moz-opacity: 0.5;
        //  filter:alpha(opacity = 50);
        cursor: default;
    }
    .tabpanel_right_scroll_disabled {
        background-position: -36px 0;
        opacity: 0.5;
        -moz-opacity: 0.5;
        //  filter:alpha(opacity = 50);

        cursor: default;
    }
    .display_none {
        display: none;
    }
</style>